package service

import (
	"context"
	"go-shop-admin/internal/dao"
	"go-shop-admin/internal/model"
	"go-shop-admin/pkg/e"
	"go-shop-admin/pkg/serializer"
	"go-shop-admin/pkg/zydhlog"
)

//角色

type AuthRoleService struct {
	Name string `form:"name" json:"name" binding:"required"` //角色名称
	Desc string `form:"desc" json:"desc"`                    //描述
}

// Add 新增
func (s *AuthRoleService) Add(ctx context.Context) *serializer.Response {
	authRoleDao := dao.NewAuthRoleDao(ctx)

	authRoleCreate := &model.AuthRole{
		Name: s.Name,
		Desc: s.Desc,
	}

	//新增
	if err := authRoleDao.AuthRoleAdd(authRoleCreate); err != nil {
		zydhlog.Error(err.Error())
		return serializer.NewResponse(e.Error, err.Error(), e.TipsGeneral, nil)
	}

	return serializer.NewResponse(e.Success, e.TipsOk, e.TipsSuccessCreate, nil)

}

// Update 更新
func (s *AuthRoleService) Update(ctx context.Context, id uint) *serializer.Response {
	authRoleDao := dao.NewAuthRoleDao(ctx)

	var authRole *model.AuthRole
	var err error

	authRole, err = authRoleDao.GetById(id)
	if err != nil {
		zydhlog.Error(err.Error())
		return serializer.NewResponse(e.ErrorExistAuthNotFind, err.Error(), e.TipsGeneral, nil)
	}

	//authRoleUpdate := &model.AuthRole{
	//	Name: s.Name,
	//	Desc: s.Desc,
	//}

	authRole.Name = s.Name
	authRole.Desc = s.Desc

	//更新
	if err = authRoleDao.AuthRoleUpdate(id, authRole); err != nil {
		zydhlog.Error(err.Error())
		return serializer.NewResponse(e.Error, err.Error(), e.TipsGeneral, nil)
	}

	return serializer.NewResponse(e.Success, e.TipsOk, e.TipSuccessUpdate, nil)

}

// Show 更新
func (s *AuthRoleService) Show(ctx context.Context, id uint) *serializer.Response {
	authRoleDao := dao.NewAuthRoleDao(ctx)

	var authRole *model.AuthRole
	var err error

	authRole, err = authRoleDao.GetById(id)
	if err != nil {
		zydhlog.Error(err.Error())
		return serializer.NewResponse(e.ErrorExistAuthNotFind, err.Error(), e.TipsGeneral, nil)
	}

	return serializer.NewResponse(e.Success, e.TipsOk, e.TipSuccessUpdate, serializer.BuildAuthRole(authRole))
}

// Delete 删除
func (s *AuthRoleService) Delete(ctx context.Context, ids []string) *serializer.Response {
	authRoleDao := dao.NewAuthRoleDao(ctx)

	if err := authRoleDao.DeleteByIds(ids); err != nil {
		zydhlog.Error(err.Error())
		return serializer.NewResponse(e.ErrorExistAuthNotFind, err.Error(), e.TipsGeneral, nil)
	}

	return serializer.NewResponse(e.Success, e.TipsOk, e.TipSuccessDelete, nil)
}
